package com.project.frba.bifront.client;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Ext;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Component;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.event.PanelListenerAdapter;
import com.gwtext.client.widgets.menu.BaseItem;
import com.gwtext.client.widgets.menu.Item;
import com.gwtext.client.widgets.menu.Menu;
import com.gwtext.client.widgets.menu.event.BaseItemListenerAdapter;
import com.project.frba.bifront.client.model.FrontDescriptor;
import com.project.frba.bifront.client.rpc.FrontGeneralServices;
import com.project.frba.bifront.client.rpc.FrontGeneralServicesAsync;
import com.project.frba.bifront.client.rpc.UserServices;
import com.project.frba.bifront.client.rpc.UserServicesAsync;
import com.project.frba.bifront.client.view.CubePanel;
import com.project.frba.bifront.client.view.HelpWindow;
import com.project.frba.bifront.client.view.MessageWindow;
import com.project.frba.bifront.client.view.dashboard.DashboardList;
import com.project.frba.bifront.client.view.dashboard.DashboardMainPanel;
import com.project.frba.bifront.client.view.descriptors.DescriptorList;
import com.project.frba.bifront.client.view.users.UserList;

public class BiPanel extends Composite{

	private GwtFront parent;
	private Panel mainPanel = new Panel();
	
	private Panel menuPanel = new Panel(); //the MenuPanel
	private TabPanel tabPanel = new TabPanel(); // the TabPanel
	
	private Menu menuInicio = new Menu();
	private Toolbar toolbar = new Toolbar();
	private ToolbarButton tbInicio = new ToolbarButton("Inicio");
	private ToolbarButton tbSalir = new ToolbarButton("Salir");
	private ToolbarButton tbAyuda = new ToolbarButton("Ayuda");
	private DashboardMainPanel dashboardPanel = new DashboardMainPanel();
	private Map<String, FrontDescriptor> descriptors = new HashMap<String, FrontDescriptor>();
	private Boolean isAdmin = false;
	
	
	public BiPanel(GwtFront parent, Boolean isAdmin){
		this.mainPanel.setId("BiPanel");
		this.parent = parent;
		this.isAdmin = isAdmin;
		loadDescriptors();
		loading();
		initWidget(mainPanel);
	}
	
	private void loading() {
		menuPanel.setBorder(false);
		menuPanel.setWidth("100%");
		menuPanel.add(toolbar);
		
		tabPanel.setBorder(false);
		tabPanel.setActiveTab(0);
		tbInicio.setMenu(menuInicio);
		toolbar.addButton(tbInicio);
		
		
		ToolbarButton tbAdmin = new ToolbarButton("Administracion");
		Menu menuAdmin = new Menu();
		tbAdmin.setMenu(menuAdmin);
		toolbar.addButton(tbAdmin);
		
		if (isAdmin) {
			Item userListItem = new Item("Usuarios");
			userListItem.addListener(new BaseItemListenerAdapter() {
				public void onClick(BaseItem item, EventObject e) {
					new UserList();
				}
			});
			userListItem.setIcon("./icons/userListItem.png");
			menuAdmin.addItem(userListItem);
			
			Item descriptorListItem  = new Item("Descriptores");
			final BiPanel me = this;
			descriptorListItem.addListener(new BaseItemListenerAdapter() {
				public void onClick(BaseItem item, EventObject e) {
					new DescriptorList(me);
				}
			});
			descriptorListItem.setIcon("./icons/descriptorListItem.png");
			menuAdmin.addItem(descriptorListItem);			
		}
		
		
		Item dashboardListItem = new Item("Tableros");
		final BiPanel me = this;
		dashboardListItem.addListener(new BaseItemListenerAdapter() {
			public void onClick(BaseItem item, EventObject e) {
				new DashboardList(me);
			}
		});
		dashboardListItem.setIcon("./icons/dashboardListItem.png");
		menuAdmin.addItem(dashboardListItem);
		
		toolbar.addButton(tbAyuda);
		tbAyuda.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				new HelpWindow();
			}
		});
		
		
		toolbar.addButton(tbSalir);
		dashboardPanel.loadDashboard();
		dashboardPanel.addListener(new PanelListenerAdapter(){
			public void onShow(Component component) {
				dashboardPanel.getDashboards();
				dashboardPanel.refreshDashboard();
			}
		});
		tabPanel.add(dashboardPanel);
				
		tbSalir.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				AsyncCallback<Boolean> cb = new AsyncCallback<Boolean>(){
					public void onFailure(Throwable caught) {
						new MessageWindow("Ha ocurrido un error al salir de la aplicación.");
					}
					
					public void onSuccess(Boolean result) {
						if(true){
							parent.logout();
							mainPanel.destroy();
						}
					}
				};
				UserServicesAsync userServices = (UserServicesAsync)GWT.create(UserServices.class);
				userServices.logout(cb);
			}
		});
		
		mainPanel.add(menuPanel);
		mainPanel.add(tabPanel);
	}
	
	public void loadMenus(){
		menuInicio.removeAll();
		for (final String descriptorName : descriptors.keySet()) {
			final FrontDescriptor descriptor = descriptors.get(descriptorName);
			Item openWindowItem = new Item(descriptor.getLabel());
			openWindowItem.setIcon("./icons/descriptorItem.png");
			menuInicio.addItem(openWindowItem);						
			openWindowItem.addListener(new BaseItemListenerAdapter(){
				public void onClick(BaseItem item, EventObject e) {
					CubePanel cube = new CubePanel(descriptors.get(descriptorName));
					tabPanel.add(cube);
					tabPanel.activate(cube.getId());
				}
			});
		}
	}
	
	public void loadDescriptors(){
		Ext.get("app").mask("espere...");
		descriptors.clear();
		AsyncCallback<Collection<FrontDescriptor>> acLoading = new AsyncCallback<Collection<FrontDescriptor>>(){
			public void onFailure(Throwable caught) {
				 new MessageWindow("No se pudieron cargar los descriptores.");
			}

			public void onSuccess(Collection<FrontDescriptor> frontDescriptors) {
				for (FrontDescriptor fDescriptor : frontDescriptors) {
					descriptors.put(fDescriptor.getDescriptorName(), fDescriptor);
				}
				loadMenus();
				Ext.get("app").unmask();
			}	
		};
		
		FrontGeneralServicesAsync frontGeneralServices = (FrontGeneralServicesAsync)GWT.create(FrontGeneralServices.class);
		frontGeneralServices.getDescriptors(acLoading);
	}
	
	public Map<String, FrontDescriptor> getDescriptors(){
		return this.descriptors;
	}

	public void refreshDashboards() {
		dashboardPanel.getDashboards();
		dashboardPanel.refreshDashboard();
	}
}
